package com.example.zhilongzhang.constructionpayapp.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.example.zhilongzhang.constructionpayapp.R;
import com.example.zhilongzhang.constructionpayapp.adapter.StatAdapter;
import com.example.zhilongzhang.constructionpayapp.base.BaseActivity;
import com.example.zhilongzhang.constructionpayapp.base.ToastManager;
import com.example.zhilongzhang.constructionpayapp.bean.MeXYBean;
import com.example.zhilongzhang.constructionpayapp.bean.StatBean;
import com.example.zhilongzhang.constructionpayapp.http.HttpMethod;
import com.example.zhilongzhang.constructionpayapp.method.NoScrollViewPager;
import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.callback.StringCallback;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class StatisticalActivity extends BaseActivity {
    NoScrollViewPager pager;
    StatAdapter adapter;
    List<MeXYBean> list = new ArrayList<>();
    String strYear = "2019", strMonth = "4", oldStrYear;
    TextView tvRect, tvCirCular;
    TextView tvTime;
    Date maxDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_statistical);
        pager = findViewById(R.id.view_pager);
        tvRect = findViewById(R.id.tv_stat_rect);
        tvTime = findViewById(R.id.tv_time);
        tvCirCular = findViewById(R.id.tv_stat_circular);
        initData();


    }

    private void initData() {
        findViewById(R.id.left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        // HH:mm:ss获取当前时间
        maxDate = new Date(System.currentTimeMillis());
        tvTime.setText("    " + simpleDateFormat.format(maxDate));
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(maxDate);
        oldStrYear = strYear;
        strYear = String.valueOf(calendar.get(Calendar.YEAR));
        strMonth = String.valueOf(calendar.get(Calendar.MONTH) + 1);
        tvTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TimePickerView pvTime = new TimePickerBuilder(StatisticalActivity.this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        Log.e("dsadas", "==" + date.compareTo(maxDate));
                        Log.e("dsadas", "date==" + date);
                        Log.e("dsadas", "maxDate==" + maxDate);

                        if (getTime2(date).compareTo(maxDate) >= 0) {
                            ToastManager.getInstance(StatisticalActivity.this).showToast("您选择时间不能大于现在");
                        } else {
                            tvTime.setText("    " + getTime(date));
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTime(date);
                            oldStrYear = oldStrYear;
                            strYear = String.valueOf(calendar.get(Calendar.YEAR));
                            strMonth = String.valueOf(calendar.get(Calendar.MONTH) + 1);
                            getAnalysis();
                        }

                    }
                }).setType(new boolean[]{true, true, false, false, false, false})
                        .setContentTextSize(20).build();
                pvTime.show();
            }
        });
        getAnalysis();
    }

    private void setListener() {
        tvRect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tvRect.setBackgroundResource(R.drawable.stat_rect_back_ok);
                tvCirCular.setBackgroundResource(R.drawable.stat_circular_back_no);
                tvRect.setTextColor(Color.parseColor("#ffffff"));
                tvCirCular.setTextColor(Color.parseColor("#333333"));
                adapter.type = 0;
                pager.setCurrentItem(0);
            }
        });
        tvCirCular.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tvRect.setBackgroundResource(R.drawable.stat_rect_back_no);
                tvCirCular.setBackgroundResource(R.drawable.stat_circular_back_ok);
                tvRect.setTextColor(Color.parseColor("#333333"));
                tvCirCular.setTextColor(Color.parseColor("#ffffff"));
                adapter.type = 1;
                pager.setCurrentItem(1);
            }
        });
    }

    private StatBean bean;

    private void getAnalysis() {
        if (strYear.equals(oldStrYear) && bean != null) {
            Log.e("data123","运行有数据222");
            list.clear();
            for (int i = 0; i < bean.getData().getDaysData().get(strMonth + "月").size(); i++) {
                int index = bean.getData().getDaysData().get(strMonth + "月").get(i).length();
                list.add(new MeXYBean(Integer.parseInt(bean.getData().getDaysData().get(strMonth + "月").get(i).substring(0, index - 1)), Float.parseFloat(bean.getData().getMonthDataAmount().get(strMonth + "月").get(i))));
            }
            adapter = new StatAdapter(StatisticalActivity.this, list);
            pager.setAdapter(adapter);
            setListener();
            return;
        }
        Log.e("data123","运行有数据111");
        HttpMethod.getAnalysis(strYear, new StringCallback() {


            @Override
            public void onError(Request request, Exception e) {
                adapter = new StatAdapter(StatisticalActivity.this, list);
                pager.setAdapter(adapter);
            }

            @Override
            public void onResponse(String response) {
                if (response == null) {
                    return;
                }
                bean = new Gson().fromJson(response, StatBean.class);
                if (bean.getCode() != 0) {
                    return;
                }
                list.clear();
                Log.e("data123","运行有数据333");
                for (int i = 0; i < bean.getData().getDaysData().get(strMonth + "月").size(); i++) {
                    int index = bean.getData().getDaysData().get(strMonth + "月").get(i).length();
                    list.add(new MeXYBean(Integer.parseInt(bean.getData().getDaysData().get(strMonth + "月").get(i).substring(0, index - 1)), Float.parseFloat(bean.getData().getMonthDataAmount().get(strMonth + "月").get(i))));
                }
                adapter = new StatAdapter(StatisticalActivity.this, list);
                pager.setAdapter(adapter);
                setListener();
            }
        });
    }

    private String getTime(Date date) {
        //可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        return format.format(date);
    }

    private Date getTime2(Date date) {
        //可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        String tiem = format.format(date);
        try {
            return format.parse(tiem);
        } catch (Exception e) {
            return null;
        }
    }

}
